package com.eastfair.util;

import cn.hutool.core.date.DateTime;
import lombok.Data;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.List;

/**
 *日期工具类
 */
@Data
public class DateTimeUtil {


    /**
     * 获取本日开始时间
     * @return 开始时间
     */
    public static LocalDateTime getDayStartTime(){
        LocalDateTime startTime=LocalDateTime.parse(LocalDateTime.of(LocalDate.now(),LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        return startTime;
    }

    /**
     * 获取本日结束时间
     * @return
     */
    public static LocalDateTime getDayEndTime(){
        LocalDateTime endTime=LocalDateTime.parse(LocalDateTime.of(LocalDate.now(),LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return endTime;
    }


    /**
     * 获取本周开始时间
     * @return
     */
    public static LocalDateTime getStartWeek(){
        LocalDate today = LocalDate.now();
        LocalDate oneDayOfWeek = getOneDayOfWeek(today, 1);
        LocalDateTime startWeekTime=LocalDateTime.parse(LocalDateTime.of(oneDayOfWeek,LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return startWeekTime;
    }

    /**
     * 获取本周的结束时间
     * @return
     */
    public static LocalDateTime getEndWeek(){
        LocalDate today = LocalDate.now();
        LocalDate oneDayOfWeek = getOneDayOfWeek(today, 7);
        LocalDateTime endWeekTime=LocalDateTime.parse(LocalDateTime.of(oneDayOfWeek,LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return endWeekTime;
    }

    /**
     * 获取一周内的某一天
     * @param today 这周内任意一天的日期
     * @param day 想要获取一周中的第几天
     * @return LocalDate
     */
    private static LocalDate getOneDayOfWeek(TemporalAccessor today, int day){
        TemporalField fieldIso = WeekFields.of(DayOfWeek.MONDAY, 1).dayOfWeek();
        LocalDate localDate = LocalDate.from(today);
        return localDate.with(fieldIso, day);
    }


    /**
     * 获取本月的开始日期
     * @return
     */
    public static LocalDateTime getOneDayOfMonth(){
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfMonth());
        LocalDateTime oneDayOfMonthTime=LocalDateTime.parse(LocalDateTime.of(with,LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return oneDayOfMonthTime;
    }

    /**
     * 获取本月的结束日期
     * @return
     */
    public static LocalDateTime getEndDayOfMonth(){
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.lastDayOfMonth());
        LocalDateTime endDayOfMonthTime=LocalDateTime.parse(LocalDateTime.of(with,LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return endDayOfMonthTime;
    }



    /**
     * 获取指定某月的开始日期
     * @param month 月份
     * @return
     */
    public static String getMonthStartOneDay(int month){
        LocalDate localDate = LocalDate.now();
        return LocalDateTime.of(LocalDate.of(localDate.getYear(),month,1),LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取指定月的结束日期
     * @param month 月份
     * @return
     */
    public static String getMonthEndTime(int month){
        YearMonth yearMonth = YearMonth.of(LocalDate.now().getYear(), month);
        LocalDate localDate = yearMonth.atEndOfMonth();
        return LocalDateTime.of(localDate,LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }




    /**
     * 获取本年度开始日期
     * @return
     */
    public static LocalDateTime getOneDayOfYear(){
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfYear());
        LocalDateTime oneDayOfYearTime=LocalDateTime.parse(LocalDateTime.of(with,LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        return oneDayOfYearTime;
    }

    /**
     * 获取本年度结束日期
     * @return
     */
    public static LocalDateTime getEndDayOfYear(){
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.lastDayOfYear());
        LocalDateTime endDayOfYearTime=LocalDateTime.parse(LocalDateTime.of(with,LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return endDayOfYearTime;
    }













}
